package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenuApp;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysMenuAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 小程序菜单权限Controller
 *
 * @author zhaohongjian
 * @date 2024-02-20
 */
@Api(value = "小程序菜单权限接口", tags = {"小程序菜单权限管理"})
@RestController
@RequestMapping("/system/menuApp")
public class SysMenuAppController extends BaseController {
    @Autowired
    private ISysMenuAppService sysMenuAppService;

    /**
     * 查询小程序菜单权限列表
     */
    @ApiOperation("查询小程序菜单权限列表")
    @GetMapping("/list")
    public AjaxResult list(SysMenuApp sysMenuApp) {
        List<SysMenuApp> list = sysMenuAppService.selectSysMenuAppList(sysMenuApp);
        return success(list);
    }

    @GetMapping("/menuTree")
    public AjaxResult deptTree(SysMenuApp sysMenuApp) {
        List<SysMenuApp> list = sysMenuAppService.selectSysMenuAppList(sysMenuApp);
        return AjaxResult.success().put("data",sysMenuAppService.buildMenuTreeSelect(list));
    }

    /**
     * 导出小程序菜单权限列表
     */
    @Log(title = "小程序菜单权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMenuApp sysMenuApp) {
        List<SysMenuApp> list = sysMenuAppService.selectSysMenuAppList(sysMenuApp);
        ExcelUtil<SysMenuApp> util = new ExcelUtil<SysMenuApp>(SysMenuApp.class);
        util.exportExcel(response, list, "小程序菜单权限数据");
    }

    /**
     * 获取小程序菜单权限详细信息
     */
    @ApiOperation("获取小程序菜单权限详细信息")
    @GetMapping(value = "/{menuId}")
    public AjaxResult getInfo(@PathVariable("menuId") Long menuId) {
        return success(sysMenuAppService.selectSysMenuAppByMenuId(menuId));
    }

    /**
     * 新增小程序菜单权限
     */
    @ApiOperation("新增小程序菜单权限信息")
    @Log(title = "小程序菜单权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMenuApp sysMenuApp) {
        sysMenuApp.setCreateTime(new Date());
        sysMenuApp.setCreateBy(SecurityUtils.getUsername());
        return toAjax(sysMenuAppService.insertSysMenuApp(sysMenuApp));
    }

    /**
     * 修改小程序菜单权限
     */
    @ApiOperation("修改小程序菜单权限信息")
    @Log(title = "小程序菜单权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMenuApp sysMenuApp) {
        return toAjax(sysMenuAppService.updateSysMenuApp(sysMenuApp));
    }

    /**
     * 删除小程序菜单权限
     */
    @ApiOperation("删除小程序菜单权限信息")
    @Log(title = "小程序菜单权限", businessType = BusinessType.DELETE)
    @DeleteMapping("/{menuIds}")
    public AjaxResult remove(@PathVariable Long[] menuIds) {
        return toAjax(sysMenuAppService.deleteSysMenuAppByMenuIds(menuIds));
    }
}
